package com.example.demo;

import jakarta.servlet.http.HttpSession;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;

@RestController
@RequestMapping("/login")
public class Demo4 {

    @RequestMapping("/d1")
    public boolean d1(String username, String password, HttpSession session) {

        if (!StringUtils.hasLength(username) || !StringUtils.hasLength(password)) {
            return false;
        }
        if ("admin".equals(username) && "admin".equals(password)) {
            session.setAttribute("user", username);
            return true;

        }
        return false;
    }

    @RequestMapping("/d2")
    public String d2(HttpSession session) {
        String user = (String) session.getAttribute("user");
        if (user == null) {
            return "未登录";
        }
        return user;
    }
}


